var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    plain: false,
    disabled: false,//按钮一旦点击提交 则不能操作

    bookname:'',//书籍名称
    author: '',//作者
    totalpage:'',//总页数  
    words: '', // 总页数
    nj:'',//推荐年级
    minnum:'',//推荐每天最少读书量
    bookjj: '',//书籍简介
    reason:'',//推荐理由
    classid: '',
    usid: '',

    classarr:[],//书籍类别
    multiArray:[],
    multiIndex: [0, 0],
 

    imageurl: '',
    fm: '',//封面图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    if (usid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      this.setData({
        usid: usid
      })
    }

    //查询类别
    this.getBookClass();

  },

  //书籍名称
  booknameinput(e) {
    this.setData({
      bookname: e.detail.value
    })
  },
  //作者
  authorinput(e) {
    this.setData({
      author: e.detail.value
    })
  },
  //总页数
  totalpageinput(e) {
    if (!(/(^\+?[1-9][0-9]*$)/.test(e.detail.value))) {
      wx.showModal({
        title: '失败',
        content:"书籍总页数只能输入正整数",
        showCancel: !1,
      });
      this.setData({
        totalpage: ""
      })
      return;
    }
    this.setData({
      totalpage:e.detail.value
    })
  },


   //总字数
   wordsinput(e) {
    if (!(/(^\+?[1-9][0-9]*$)/.test(e.detail.value))) {
      wx.showModal({
        title: '失败',
        content:"书籍总字数只能输入正整数",
        showCancel: !1,
      });
      this.setData({
        words: "0"
      })
      return;
    }
    this.setData({
      words:e.detail.value
    })
  },

  //年级
  njinput(e) {
    if (!(/(^\+?[1-9][0-9]*$)/.test(e.detail.value))) {
      wx.showModal({
        title: '失败',
        content:"年级只能输入正整数",
        showCancel: !1,
      });
      this.setData({
        nj: ""
      })
      return;
    }
    this.setData({
      nj:e.detail.value
    })
  },

  //推荐每天最少读多少页
  minnuminput(e) {
    if (!(/(^\+?[1-9][0-9]*$)/.test(e.detail.value))) {
      wx.showModal({
        title: '失败',
        content:"推荐每天最少读多少页只能输入正整数",
        showCancel: !1,
      });
      this.setData({
        minnum: ""
      })
      return;
    }
    this.setData({
      minnum: e.detail.value
    })
  },

  //书籍简介
  bookjjinput(e) {
    this.setData({
      bookjj: e.detail.value
    })
  },

  //推荐理由
  reasoninput(e) {
    this.setData({
      reason: e.detail.value
    })
  },


  //上传图片
  uploadimage: function () {
    var that = this;
    wx.chooseImage({
      count: 1, //张数， 默认9
      sizeType: ['compressed'], //建议压缩图
      sourceType: ['album', 'camera'], // 来源是相册、相机
      success: function (res) {
        that.imgSecCheck(res.tempFilePaths[0]);
      }
    });
  },
  imgSecCheck(imgitem) {
    var that=this;
    wx.showLoading({
      title: '图片检测中',
    })
    const uploadTask = app.wxupload({
      url:  'upload/imgSecCheck.html',
      filePath: imgitem,
      name: 'file',
      formData: {},
      success: function (res) {
        var datas = JSON.parse(res.data);
        wx.hideLoading();
        if(!datas.result){
          wx.showModal({
            title: '失败',
            content:datas.msg,
            showCancel: !1,
          });
          return;
        }else{
          that.setData({
            imageurl: imgitem
          })
        }
      }
    })
  },

 
  //删除图片
  delImage: function (e) {
    var that = this;
    that.setData({
      imageurl:""
    })
  },

  //预览图片
  viewimage: function (e) {
    var imageurl=this.data.imageurl;
    wx.previewImage({
      current: imageurl, // 当前显示图片的http链接
      urls: [imageurl] // 需要预览的图片http链接列表
    })
  },



  sureBookxsyc: function (e) {
    var that = this;
    var bookname = that.data.bookname;
    if (bookname == '') {
      wx.showModal({
        title: '友情提示',
        content: '请填写书籍名称!',
        showCancel: !1,
      })
      return;
    }

    var words = that.data.words;
    if (words == '') {
      wx.showModal({
        title: '友情提示',
        content: '请填写书籍大概总字数!',
        showCancel: !1,
      })
      return;
    }
    
    var author = that.data.author;
    if (author == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入作者名称!',
        showCancel: !1,
      })
      return;
    }

    var multi=that.data.multiIndex;
    var multiIndex = multi[1];
    if (multiIndex == 0) {
      wx.showModal({
        title: '友情提示',
        content: '请选中书籍分类!',
        showCancel: !1,
      })
      return;
    }


    var bookjj = that.data.bookjj;
    if (bookjj == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入书籍简介!',
        showCancel: !1,
      })
      return;
    }

    var reason = that.data.reason;
    if (reason == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入推荐理由!',
        showCancel: !1,
      })
      return;
    }

    var imageurl = that.data.imageurl;
    if (imageurl == '') {
      wx.showModal({
        title: '友情提示',
        content: '请上传书籍封面，以方便展示!',
        showCancel: !1,
      })
      return;
    }
    wx.showLoading({
      title: '保存中',
    })
    that.setData({
      disabled: true,
      loading: true
    })

    //先上传图片
    if (imageurl) {
      //先上传图片
      that.uploadBookurl();
    } else {
      that.addBookJztj();
    }
  },



  //提交书籍推荐的图片
  uploadBookurl: function () {
    var that = this;
    var imageurl = that.data.imageurl;
    const uploadTask = app.wxupload({
      url:  'upload/uploadwx.html?savefile=booktj',
      filePath: imageurl,
      name: 'file',
      formData: {},
      success: function (res) {
        var img = JSON.parse(res.data);
        var url = img.urls;
        that.setData({
          fm: url
        });
        that.addBookJztj(); 
      }
    })
  },
  
  addBookJztj() {
    var that = this;
    var bookname = that.data.bookname;
    var author = that.data.author;
    var totalpage = that.data.totalpage;
    var nj = that.data.nj;
    var minnum = that.data.minnum;
    //获取分类ID
    var multiIndex = that.data.multiIndex;
    var in1 = multiIndex[0];
    var in2 = multiIndex[1];
    var classarr = this.data.classarr;
    var multi = classarr[in1].list;
    var classid = multi[(parseInt(in2)-1)].CLASSID;

    var bookjj = that.data.bookjj;
    var reason = that.data.reason;
    var recommendor = wx.getStorageSync('name');
    var recommendorid = that.data.usid;
    var fm = that.data.fm;
    var words =  that.data.words;
    var user_type = wx.getStorageSync('user_type');
    var tjly='1';
    if(user_type=='2'){
      tjly='0'
    }
    app.wxpost({
      url:  'wxsyc/addBookJztj.html',
      data: {
        bookname: bookname,
        author: author,
        totalpage:totalpage,
        nj:nj,
        minnum:minnum,
        classid: classid,
        bookjj: bookjj,
        reason: reason,
        recommendor: recommendor,
        recommendorid: recommendorid,
        fm: fm,
        tjly:tjly,
        words: words
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          disabled: false,
          loading: false
        })
        if (res.data.result) {
          wx.showModal({
            title: '成功',
            content: res.data.msg,
            showCancel: !1,
          });
          wx.redirectTo({
            url: '../../pages/xsyc/xsyc'
          })
        } else {
          wx.showModal({
            title: '失败',
            content: res.data.msg,
            showCancel: !1,
          });
        }
      }
    })
  },

  getBookClass() {
    var that = this;
    app.wxget({
      url: 'wxsyc/getBookClass.html',
      success: function (res) {
        var classarr = res.data;
        that.setData({
          classarr: classarr
        });

        var multiArray=[];
        for (var i = 0; i < classarr.length;i++){
          multiArray.push(classarr[i].CLASSNAME);
        }
        var multi = classarr[0].list;
        var multiArray2 = [];
        multiArray2.push("请选择书籍分类");
        for (var j = 0; j < multi.length; j++) {
          multiArray2.push(multi[j].CLASSNAME);
        }
        
        that.setData({
          "multiArray[0]": multiArray,
          "multiArray[1]": multiArray2
        });
      }
    })
  },


  classchange(e) {
    var column = e.detail.column;
    var index = e.detail.value;
    if (column==0){
      //选中第一列
      var classarr = this.data.classarr;
      var multi = classarr[index].list;
      var multiArray = [];
      multiArray.push("请选择书籍分类");
      for (var j = 0; j < multi.length; j++) {
        multiArray.push(multi[j].CLASSNAME);
      }
      this.setData({
        "multiArray[1]": multiArray,
        multiIndex: [index, 0],
      });
    }else{
      //选中第二列
      this.setData({
        "multiIndex[1]": index,
      });
    }
  },

  downmodel(e){
    let _that = this;
    wx.downloadFile({
      url:  app.myUlife.ulifeUrl +'/static/importbooktm.xls',
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(res)
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      }

    })

  }

})